package TSP;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TSP extends JFrame implements ActionListener
{
	JPanel pane = new JPanel();
	static TSPMap map;
	
	static private int OFFSET = 0;
	static private int SCALE = 10;
	
	JLabel enterName;
	JTextField name;
	JButton click;
	String storeName;
	
	TSP()
	{
		super("My Simple Frame"); 
		setBounds(0,0,800,800);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    Container con = this.getContentPane(); // inherit main frame
	    con.add(pane); // add the panel to frame
	    // customize panel here
	    // pane.add(someWidget);
	    
	    
	    setLayout(null);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   // enterName = new JLabel("Enter Your Name: ");
	    click = new JButton("Click");
	   // name = new JTextField();
	   // enterName.setBounds(60,30,120,30);
	    //name.setBounds(80,60,130,30);
	    click.setBounds(100,190,60,30);
	    click.addActionListener(this);
	    add(click);
	   // add(name);
	   // add(enterName);
	    
	    setVisible(true); // display this frame
	    
	}
	
	public void actionPerformed(ActionEvent e) {

	    if(e.getSource() == click) 
	    {
	    	//map.GreedyStep();
	    	repaint();
	        //storeName = name.getText();
	        //JOptionPane.showMessageDialog(null, "Hello" + storeName);
	        //System.exit(0);
	    }
	}
	@Override
	public void update(Graphics g)
	{
		super.update(g);
		
	}

	@Override
	public void paint(Graphics g)  
	{  
	 super.paint(g);  
	  
	 if (map.solved)
	 {
		 remove(click);
	 }
	 if (map != null && map.solved)
	 {
		 //set color to RED  
		 //So after this, if you draw anything, all of it's result will be RED  
		 g.setColor(Color.RED);  
		  
		 //fill circle with RED  
		  
		 for(int i = 0; i < map.nbCity; i++)
		 {
			 g.fillOval((int)map.cityList[i].posX * SCALE + OFFSET,(int)map.cityList[i].posY * SCALE + OFFSET,4,4);
		 }
		 
		 map.currentCityDebugPathId = 0;
		 for(int i  = 0; i <= map.nbCity; i++)
		 {
			 
			 
			 City c1 = map.cityList[map.currentCityDebugPathId];
			 JLabel name = new JLabel(String.valueOf(c1.originalId));
			 City c2 = map.cityList[map.currentCityDebugPathId].getNextDestination();
			 Graphics2D g2 = (Graphics2D) g;
			
			 name.setBounds((int)(0), (int)(0), (int)(20), (int)(20));
			 name.setLocation((int)(c1.posX * SCALE + OFFSET), (int)(c1.posY * SCALE + OFFSET - 30));
		     add(name);
			 Line2D lin = new Line2D.Float((c1.posX * SCALE + OFFSET), (c1.posY * SCALE + OFFSET), (c2.posX * SCALE + OFFSET), (c2.posY * SCALE + OFFSET));
		     g2.draw(lin);
		     
		     map.currentCityDebugPathId = c2.originalId;
		 }
	 }
	}  
	
	public static void main(String[] args) 
	{

		new TSP();
		try 
		{
			if (args.length > 0)
			{
				doSolve("G:/discrete-optimization/travelingSalesman/tmp.data");
			}
			else
			{
				//doSolve("C:/dev/coursera/travelingSalesman/tmp.data");
				doSolve("G:/discrete-optimization/travelingSalesman/data/tsp_724_1");
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}

	}
	
	public static void doSolve(String file) throws IOException
	{
		map = RawDataHandler.ParseData(file);
		map.SetNeighbour();
		map.CalculateNeighbourDistance();
		
		map.Solve();
		map.solved = true;
		
		RawDataHandler.OutputSolution(map);

		
	}

}
